<template>
  <div>
    <el-page-header @back="$router.back()" content="轮播列表" style="margin-bottom: 20px"/>
    <el-form :model="searchFrom" ref="searchForm" inline>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchFrom.status" clearable>
          <el-option :value="1" label="可用"/>
          <el-option :value="2" label="禁用"/>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="searchFrom.type" clearable>
          <el-option :value="1" label="首页广告"/>
          <el-option :value="2" label="其他广告"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button type="warning" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-bottom: 15px">
      <el-button @click="openWin()">添加</el-button>
    </div>
    <el-table :data="dataList" border>
      <el-table-column label="编号" prop="bannerId"/>
      <el-table-column label="广告名称" prop="bannerName"/>
      <el-table-column label="跳转连接" prop="bannerHref"/>
      <el-table-column label="类型" prop="type">
        <template slot-scope="scope">
          <el-tag effect="dark" type="info" v-if="scope.row.type === 1">首页</el-tag>
          <el-tag effect="dark" type="info" v-else>其他</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 1" size="small" type="success" @click="changeStatus(scope.row.bannerId,2)">可用</el-button>
          <el-button v-else type="danger" size="small" @click="changeStatus(scope.row.bannerId,1)">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="排序值" prop="orderNum">
        <template slot-scope="scope">
          <el-input v-model="scope.row.orderNum">
            <el-button slot="append" icon="el-icon-check" @click="changeOrderNum(scope.row.bannerId,scope.row.orderNum)"></el-button>
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="openWin(scope.row.bannerId)">修改</el-button>
          <el-button type="warning" size="small" @click="delData(scope.row.bannerId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :page-size="searchFrom.limit" :current-page="searchFrom.page" :page-sizes="[5,10,30,50,100]" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    <BannerUpdate ref="BannerUpdate" @closeWin="getDataList"/>
  </div>
</template>

<script>
import BannerUpdate from '@/views/banner/BannerUpdate'
export default {
  name: 'UserinfoList',
  components: { BannerUpdate },
  data () {
    return {
      searchFrom: {
        page: 1,
        limit: 5,
        status: '',
        type: ''
      },
      dataList: [],
      total: 0
    }
  },
  methods: {
    resetForm () {
      this.$refs.searchForm.resetFields()
    },
    delData (bannerId) {
      this.$confirm(`确定删除编号[${bannerId}]的值吗?`, { type: 'info' }).then(() => {
        this.$http.delete(`banner/delete/${bannerId}`).then(() => {
          this.$alert('删除数据成功！', { type: 'success' }).then(() => {
            this.getDataList()
          })
        })
      })
    },
    openWin (bannerId) {
      this.$refs.BannerUpdate.init(bannerId)
    },
    getDataList () {
      this.$http.get('banner/page', this.searchFrom).then(data => {
        this.dataList = [...data.data.list]
        this.total = data.data.total
      })
    },
    handleSizeChange (val) {
      this.searchFrom.limit = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.searchFrom.page = val
      this.getDataList()
    },
    changeStatus (bannerId, status) {
      this.$confirm(`确定更改编号[${bannerId}]的状态为${status === 1 ? '[<span style="color: green">可用</span>]' : '[<span style="color: red">禁用</span>]'}吗？`, { dangerouslyUseHTMLString: true }).then(() => {
        this.$http.post('banner/status', { bannerId, status }).then(() => {
          this.$message.success('更改状态完成！')
          this.getDataList()
        })
      })
    },
    changeOrderNum (bannerId, orderNum) {
      this.$http.post('banner/order/num', { bannerId, orderNum }).then(() => {
        this.$message.success('更新排序值成功')
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.getDataList()
    })
  }
}
</script>

<style scoped>

</style>
